package com.xxxx.portal.controller;

/**
 * @author nathan-wang on 2020-11-04
 */

import com.xxxx.api.service.ICheckCaptchaService;
import com.xxxx.api.service.IRegisterService;
import com.xxxx.api.service.SSOService;
import com.xxxx.api.pojo.Admin;
import com.xxxx.api.pojo.RegisterParams;
import com.xxxx.api.pojo.ResultInfo;
import com.xxxx.api.utils.CookieUtil;
import com.xxxx.api.utils.RespBean;

import com.xxxx.api.utils.UserIDBase64;
import com.xxxx.portal.service.CookieService;
import com.xxxx.portal.util.SendMsgUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author : ymd  & nathan-wang
 * @Date : 2020/11/2 0002 16:00
 * @Version 1.0
 * @Description : 用户控制层
 */
@Controller
public class UserController {
    @Reference(version = "1.0")
    private IRegisterService registerService;
    @Reference(version = "1.0")
    private ICheckCaptchaService checkCaptchaService;
    @Reference(version = "1.0")
    private SSOService ssoService;
    @Resource
    private CookieService cookieService;
    @Resource
    private SendMsgUtil sendMsgUtil;

    @RequestMapping("/register")
    public String toRegister (){
        return "register";
    }
    /**
     * 用户注册
     *
     * @param
     * @return
     */
    @PostMapping("/user/register")
    @ResponseBody
    public ResultInfo register( @RequestParam("phone") String phone, @RequestParam("userName") String userName, @RequestParam("password") String password,@RequestParam("pwdConfirm") String pwdConfirm) {

        RegisterParams registerParams = new RegisterParams();
        registerParams.setUserName(userName);
        registerParams.setPhone(phone);
        registerParams.setPassword(password);
        registerParams.setPwdConfirm(pwdConfirm);
        ResultInfo resultInfo = registerService.register(registerParams);
        sendMsgUtil.send(phone);
        return resultInfo;
    }
    /**
     * 校验用户的验证码票据
     *
     * @param ticket
     * @param randstr
     * @return
     */
    @PostMapping("/getCaptcha")
    @ResponseBody
    public ResultInfo checkCaptcha(@RequestParam("ticket") String ticket, @RequestParam("randstr") String randstr) {
        ResultInfo resultInfo = checkCaptchaService.checkCaptcha(ticket,randstr);
        return resultInfo;
    }
    @RequestMapping("/login")
    public String loginIndex (){
        return "login";
    }

    @ResponseBody
    @RequestMapping("/user/login1")
    public RespBean login (Admin admin, HttpServletRequest request, HttpServletResponse response){
        //生成信息
        String ticket = ssoService.Login(admin);
        //如果有信息
        if (!StringUtils.isEmpty(ticket)){
            boolean result = cookieService.setCookie(request, response, ticket);
            request.getSession().setAttribute("user", admin);
            //设置用户id的cookie
            String id = ssoService.queryByName(admin.getUserName()).getAdminId().toString();
            int i = Integer.parseInt(id);
            String s = UserIDBase64.encoderUserID(i);
            CookieUtil.setCookieNoEncode("userId", s, "", response);
            return result?RespBean.success("登录成功！"):RespBean.error("登录失败！");
        }
        return RespBean.error("登录失败！");
    }

    @GetMapping("/index")
    public String index (HttpServletRequest request,HttpServletResponse response){
        //获取用户信息
        String ticket = CookieUtil.getCookieValue(request, "ticket");
        if (!StringUtils.isEmpty(ticket)){
            //查询用户
            Admin admin = ssoService.validate(ticket);
            //设置会话
            request.getSession().setAttribute("user", admin);
        }
        return "index";
    }

    @RequestMapping("/logout")
    public String logout (HttpServletRequest request,HttpServletResponse response){
        //获取票据
        String ticket = cookieService.getCookie(request);
        //redis删除票据
        ssoService.logout(ticket);
        //移除session会话中的user对象
        request.getSession().removeAttribute("user");
        //浏览器删除cookie
        cookieService.deleteCookie(request, response);
        return "login";
    }

    /**
     * 订单拦截登录跳转
     * @param redirectUrl
     * @param model
     * @return
     */
    @RequestMapping("/login1")
    public String login(String redirectUrl, Model model){
        System.out.println(redirectUrl);
        model.addAttribute("redirectUrl",redirectUrl);
        return "login";
    }
}

